package com.chery.media.ui.play.usb

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.chery.media.R
import com.chery.media.contacts.EventContacts
import com.chery.media.contacts.MusicType
import com.chery.media.databinding.FragmentUsbBinding
import com.chery.media.library.base.BaseVMFragment
import com.chery.media.library.extend.clickNoRepeat
import com.chery.media.library.extend.loadPicture
import com.chery.media.library.extend.toTime
import com.chery.media.ui.play.playViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class UsbFragment : BaseVMFragment<UsbViewModel, FragmentUsbBinding>() {

    private var isResume = false

    private val usbAdapter by lazy {
        UsbAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                val item = getItem(position)
                playViewModel.playMusic(position)
            }
        }
    }

    override val layoutId: Int
        get() = R.layout.fragment_usb


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.musicListRecycler.adapter = usbAdapter
    }

    override fun onResume() {
        super.onResume()
        isResume = true
        lifecycleScope.launch(Dispatchers.IO) {
            if (playViewModel.currentType.value == MusicType.USB) {
                playViewModel.bluetoothPlayMusic(true)
                playViewModel.servicePlayMusic(true)
                if (viewModel.usbMusicList.value.isNullOrEmpty()) {
                    viewModel.getUsbMusicList()
                } else {
                    playViewModel.setPlayList(viewModel.usbMusicList.value ?: arrayListOf())
                    playViewModel.setCurrentPlayMusic()
                }
            }
        }
//        if (playViewModel.currentType.value == MusicType.USB) {
//            playViewModel.bluetoothPlayMusic(true)
//            playViewModel.servicePlayMusic(true)
//            if (viewModel.usbMusicList.value.isNullOrEmpty()) {
//                viewModel.getUsbMusicList()
//            } else {
//                playViewModel.setPlayList(viewModel.usbMusicList.value ?: arrayListOf())
//                playViewModel.setCurrentPlayMusic()
//            }
//        }
    }

    override fun initObserve() {
        // USB状态
        LiveEventBus.get<Boolean>(EventContacts.USB_STATUS).observe(this) {
            viewModel.getUsbMusicList()
        }
        // USB音乐列表
        viewModel.usbMusicList.observe(this) {
            usbAdapter.setList(it)
            playViewModel.setPlayList(it)
            playViewModel.setCurrentPlayMusic()
        }
        // 显示当前的音乐信息、更新当前播放的音乐进度
        playViewModel.currentUsbPlayMusic.observe(this) {
            if (it.isPlaying == true) {
                mBinding.pause.setImageResource(R.drawable.svg_play_pause)
            } else {
                mBinding.pause.setImageResource(R.drawable.svg_play)
            }
            mBinding.musicName.text = it.musicName
            mBinding.musicAuthor.text = it.musicAuthor
            val totalTime = (it.totalTime ?: "0").toFloat()
            mBinding.totalTime.text = (it.totalTime ?: "0").toTime()
            mBinding.musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
            if (playViewModel.currentType.value == MusicType.BLUETOOTH) {
                mBinding.musicBluetoothLyrics.text = ""
            } else {
                mBinding.musicBluetoothLyrics.text = ""
            }
            mBinding.playPosition.text = (it.playPosition ?: "0").toTime()
            val seekBarProgress = ((it.playPosition ?: "0").toFloat() / totalTime) * 100
            mBinding.musicSeeKBar.progress = seekBarProgress.toInt()
        }
    }

    override fun initListener() {
        // 上一首
        mBinding.previousSong.clickNoRepeat(interval = 0) {
            playViewModel.playPrevious()
        }
        // 播放暂停
        mBinding.pause.clickNoRepeat(interval = 0) {
            playViewModel.continuePlayOrPause()
        }
        // 下一首
        mBinding.nextSong.clickNoRepeat(interval = 0) {
            playViewModel.playNext()
        }
        // 播放列表
        mBinding.playList.clickNoRepeat(interval = 0) {
            if (mBinding.musicListRecycler.isVisible) {
                mBinding.infoLayout.isVisible = true
                mBinding.musicListRecycler.isVisible = false
            } else {
                mBinding.infoLayout.isVisible = false
                mBinding.musicListRecycler.isVisible = true
            }
        }
    }

    companion object {
        fun newInstance() = UsbFragment()
    }

}